const app = getApp()
//引入库文件
const Multipart = require('../../utils/Multipart.min.js')

Page({
  data: {
    files: []
  },
  chooseImage: function(e) {
    var that = this
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFiles = res.tempFilePaths.map(tempPath => ({
          filePath: tempPath,
          name: tempPath.name
        }))
        that.setData({
          files: that.data.files.concat(tempFiles)
        })
      }
    })
  },
  multipart2() {
    wx.navigateTo({
      url: '../multipart/multipart2',
    })
  },
  formSubmit(e) {
    let body = e.detail.value
    let fields = Object.keys(body).map(key => ({
      name: key,
      value: body[key]
    }))
    let files = this.data.files
    console.log(files)
    console.log(fields)
    new Multipart({
      files,
      fields
    }).submit('http://localhost:3000/upload', {
      header: {
        'Cookie': 'name=1'
      }
    }).then(res=>{
      console.log(res);
      wx.showToast({
        title: 'over',
      })
    },err=>{
      console.log(err)
    })
  }
})